package com.lishe.lishe.lishe.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.R;


/**
 * Created by lwa on 2017/8/16.
 */

public class MessageDialog extends Dialog {
    protected MessageDialog(Context context) {
        super(context);
    }

    protected MessageDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected MessageDialog(Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }


    public static class Builder {
        private Context context;
        private String strContent;
        private String strTitle;
        private String strNegative;
        private String strPositive;
        private ViewGroup.LayoutParams params;
        private int contentGravity= Gravity.CENTER_VERTICAL;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 内容
         * @param msg
         * @return
         */
        public Builder setContent(String msg){
            strContent=msg;
            return this;
        }
        /**
         * 内容位置
         * @param gravity
         * @return
         */
        public Builder setContentGravity(int gravity){
            contentGravity=gravity;
            return this;
        }
        /**
         * 内容
         * @param id
         * @return
         */
        public Builder setContent(int id){
            strContent=context.getString(id);
            return this;
        }

        /**
         * 标题
         * @param msg
         * @return
         */
        public Builder setTitle(String msg){
            strTitle=msg;
            return this;
        }
        /**
         * 标题
         * @param id
         * @return
         */
        public Builder setTitle(int id){
            strTitle=context.getString(id);
            return this;
        }

        /**
         * 取消按钮
         * @param msg
         * @return
         */
        public Builder setPositiveButton(String msg, OnPositiveListener onPositiveListener){
            strPositive=msg;
            this.onPositiveListener=onPositiveListener;
            return this;
        }
        /**
         * 确认按钮
         * @param id
         * @return
         */
        public Builder setPositiveButton(int id, OnPositiveListener onPositiveListener){
            strPositive=context.getString(id);
            this.onPositiveListener=onPositiveListener;
            return this;
        }
        /**
         * 设置parms
         * @return
         */
        public Builder setDialogParms(WindowManager.LayoutParams parms) {
            this.params=parms;
            return this;
        }
        /**
         * 设置parms
         * @return
         */
        public Builder setDialogParms(int width,int height) {
            this.params=new WindowManager.LayoutParams();
            params.width=width;
            params.height=height;
            return this;
        }


        public MessageDialog create() {
            final MessageDialog dialog = new MessageDialog(context, R.style.MessageDialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.dialog_message, null);
            dialog.addContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            LinearLayout titleLayout = view.findViewById(R.id.title_layout);
            TextView title = view.findViewById(R.id.title);
            TextView content = view.findViewById(R.id.content);
            content.setGravity(contentGravity);
            TextView positive = view.findViewById(R.id.positive);
            if(params==null){
                WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
                params.width = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth()/4 * 3;//4分之3的宽度
//                params.height = 200 ;
                dialog.getWindow().setAttributes(params);
            }
            if(strTitle==null){
                titleLayout.setVisibility(View.GONE);
            }else{
                titleLayout.setVisibility(View.VISIBLE);
                title.setText(strTitle);
            }
            if(content!=null){
               content.setText(strContent);
            }

            if(strPositive!=null){
               positive.setText(strPositive);
            }
            positive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    dialog.dismiss();
                    if(onPositiveListener!=null){
                        onPositiveListener.onPositive(dialog);
                    }
                }
            });
            return dialog;
        }

        public void show(){
            create().show();
        }


        OnPositiveListener onPositiveListener;

        public interface OnPositiveListener {
            void onPositive(MessageDialog dialog);
        }



        public void setOnNegativeListener(OnPositiveListener onPositiveListener) {
            this.onPositiveListener = onPositiveListener;
        }
    }
}
